 /* Copyright (C)
  * 2024 ZKLX Optoelectronic Technology Co.LTD
  * All rights reserved.
  */
 /**
  * @file main.cpp
  * @brief jvcp example app
  * @author xuke
  * @version 1.0
  * @date 2024-09-28
  */

#include <iostream>
#include <thread>
#include <unistd.h>
#include <opencv2/opencv.hpp>
#include <cJSON.h>
#include "jcvp.h"
#include "/home/xuke/bin/dbg.h"

/*--------------------------------extern--------------------------------------------*/

/*--------------------------------globals-------------------------------------------*/

/*--------------------------------locals--------------------------------------------*/

/*--------------------------------funcs---------------------------------------------*/
/**
 * @brief video模拟线程
 * @param conn
 */
void frame_capture_task(void *arg)
{
	cv::Mat frame;
	JCVP_T *jcvp_obj = (JCVP_T*)arg;
	if (!jcvp_obj) {
		my_debug_red_msg("jcvp_obj is NULL");
		return;
	}

	while (true) {
		cv::VideoCapture capture("/home/xuke/tftpboot/rl_test_2_raw.mp4");
		if (!capture.isOpened()) {
			std::cerr << "无法打开视频文件" << std::endl;
			return;
		}

		while (capture.read(frame)) {
			jcvp_obj->frame_update(frame);
			usleep(20*1000); //50HZ
		}
		capture.release();
	}
}

/**
 * @brief 状态上报
 * @param arg
 */
void status_update_task(void *arg)
{
	JCVP_T *jcvp_obj = (JCVP_T*)arg;
	if (!jcvp_obj) {
		my_debug_red_msg("jcvp_obj is NULL");
		return;
	}

	char date_str[32] = {0};
	char time_str[32] = {0};
	while (true) {
		time_t cur_time = time(NULL);
		strftime(date_str, sizeof(date_str), "%Y-%m-%d", localtime(&cur_time));
		strftime(time_str, sizeof(time_str), "%H:%M:%S", localtime(&cur_time));

		cJSON *root = cJSON_CreateObject();
		cJSON_AddStringToObject(root, "type", "status");
		cJSON_AddStringToObject(root, "date", date_str);
		cJSON_AddStringToObject(root, "time", time_str);

		char *root_str = cJSON_Print(root);
		jcvp_obj->jc_send(std::string(root_str));
		free(root_str);
		sleep(1);
	}
}

/**
 * @brief main
 * @param argc
 * @param argv[]
 * @return 0
 */
int main(int argc, const char *argv[])
{
	local_test_main(argc, argv);
	return 0;
}

